home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Systemmonitors / LAV / lav.c < prev    next >
C/C++ Source or Header  |  1996-09-26  |  5KB  |  203 lines

  1. /*
  2.  *    lav.c - Amiga load average
  3.  *
  4.  *    Written by:
  5.  *    William J. Rucklidge
  6.  *    wjr@unicus.UUCP        (soon to be wjr@unicus.com)
  7.  *
  8.  *    You may take this program and skywrite it and sell tickets for all
  9.  *    I care. (i.e this program is placed in the public domain).
  10.  *    However, if you have any improvements, suggestions or comments, mail
  11.  *    a note to me.
  12.  *
  13.  *      Modified to be font sensitive and "2.0ish" by Eric G. Suchanek, Ph.D.,
  14.  *      BIX: esuchanek
  15.  *      INET: procter!suchanek_eg@ms.uky.edu
  16.  *      12/29/92
  17.  */
  18.  
  19. #include    <intuition/intuition.h>
  20. #include    <exec/exec.h>
  21. #include    <exec/execbase.h>
  22.  
  23. #define    TIME15    900    /* Number of samples in 15 minutes */
  24. #define    TIME5    300    /* Number of samples in 5 minutes */
  25. #define    TIME1    60    /* Number of samples in 1 minute */
  26. #define    TPS    50    /* Ticks per sample, for Delay() */
  27. #define    CLOSEWIDTH    30    /* Width of the close gadget in pixels */
  28. #define WDRAGBARLEN     40      /* Width of close and drag gadget */
  29. #define INTUI_REV       37L
  30.  
  31. UBYTE MyIdent[] = "$VER: LAV 1.1 " __DATE__ " ";
  32. UBYTE MyScreenTitle[] = "LAV 1.1 William J. Rucklidge and Eric G. Suchanek, Ph.D. "__DATE__ " ";
  33.  
  34. struct NewWindow windef = 
  35. {
  36.     430, 0, 240, 12,
  37.     -1, -1,
  38.     CLOSEWINDOW,
  39.     WINDOWCLOSE | WINDOWDRAG,
  40.     NULL, NULL,
  41.     (UBYTE *) "    ",
  42.     NULL, NULL, 0, 0, 0, 0,
  43.     WBENCHSCREEN
  44. };
  45.  
  46.  
  47. extern void *OpenLibrary(), *OpenWindow(), *FindTask();
  48.  
  49. struct IntuitionBase *IntuitionBase;
  50.  
  51. void convertTime(long sum, int count, register char *position)
  52. {
  53.     register long temp;
  54.     
  55.     temp = (sum * 100L + 50L) / count;
  56.     *(position + 4) = '0' + (int)(temp % 10);
  57.     temp /= 10;
  58.     *(position + 3) = '0' + (int)(temp % 10);
  59.     temp /= 10;
  60.     *(position + 1) = '0' + (int)(temp % 10);
  61.     temp /= 10;
  62.     if (temp != 0) 
  63.       {
  64.     *position = '0' + (int)(temp % 10);
  65.       }
  66.     else 
  67.       {
  68.     *position = ' ';
  69.       }
  70.   }
  71.  
  72. #define    FORMAT    "Load: ##.## ##.## ##.##"
  73.  
  74. main()
  75. {
  76.   register int length;    /* Length of the current run queue */
  77.   register struct Node *mynode; /* Node to walk through the queue */
  78.   register int i;    /* General counter */
  79.   int last[TIME15];    /* Length of the run queue in the last TIME seconds */
  80.   long sum15 = 0;    /* Sum of the last 15 minutes */
  81.   long sum5 = 0;    /* Sum of the last 5 minutes */
  82.   long sum1 = 0;    /* Sum of the last minute */
  83.   int pos = 0;    /* Current position in last for new value */
  84.   int pos15 = 0;    /* Current position in last for sum15 */
  85.   int pos5 = TIME15-TIME5;    /* Current position in last for sum5 */
  86.   int pos1 = TIME15-TIME1;    /* Current position in last for sum1 */
  87.   char uptimestr[26];
  88.   struct ExecBase *ExecBase;
  89.   struct Window *win;
  90.   struct Screen *myscr = NULL;
  91.     
  92.   struct RastPort tmprp;   /* RastPort for font-sensitive trick */
  93.   struct TextFont *f;
  94.   ULONG ww = 0;
  95.   
  96.   SetTaskPri(FindTask(0L), 5L);
  97.   
  98.   (void) strcpy(uptimestr, FORMAT);
  99.   
  100.   if (!(ExecBase = (struct ExecBase *)OpenLibrary("exec.library", INTUI_REV))) {
  101.     VPrintf("Couldn't open exec... the sky is falling!\n");
  102.     exit(999);
  103.   }
  104.   
  105.   if (!(IntuitionBase =
  106.     (struct IntuitionBase *)OpenLibrary("intuition.library", INTUI_REV))) 
  107.     {
  108.       VPrintf("Couldn't open intuition - must be dumb\n");
  109.       CloseLibrary(ExecBase);
  110.       exit(998);
  111.     }
  112.   
  113.   if (!(myscr = LockPubScreen(NULL)))
  114.     {
  115.       VPrintf("Couldn't lock Workbench screen");
  116.       CloseLibrary(IntuitionBase);
  117.       CloseLibrary(ExecBase);
  118.       exit(996);
  119.     }
  120.   
  121.   
  122.   if (!(f=OpenFont(myscr->Font))) goto done;
  123.   InitRastPort(&tmprp);
  124.   SetFont(&tmprp,f);
  125.   ww=TextLength(&tmprp,FORMAT,strlen(FORMAT));
  126.   CloseFont(f);
  127.   
  128.   if (!(win = OpenWindowTags(&windef,
  129.                  WA_Height, myscr->BarHeight + 1, 
  130.                  WA_Width, ww+WDRAGBARLEN,
  131.                  WA_PubScreen, myscr, TAG_END))) 
  132.     {
  133.       printf("Couldn't open window\n");
  134.       CloseLibrary(IntuitionBase);
  135.       CloseLibrary(ExecBase);
  136.       exit(997);
  137.     }
  138.   
  139.   UnlockPubScreen(NULL);
  140.   
  141.   
  142.   for (i = 0; i < TIME15 ; i++) 
  143.     {
  144.       last[i] = 0;
  145.     }
  146.   
  147.   for (;;) 
  148.     {
  149.       Delay((long)TPS);
  150.       if (GetMsg(win -> UserPort)) 
  151.     {
  152.       break;
  153.     }
  154.     
  155.       length = 0;
  156.       Disable();
  157.       for (mynode = (ExecBase -> TaskReady).lh_Head;
  158.        mynode = mynode -> ln_Succ; ) {
  159.     length++;
  160.       }
  161.       Enable();
  162.     
  163.       sum15 += (length - last[pos15]);
  164.       sum5 += (length - last[pos5]);
  165.       sum1 += (length - last[pos1]);
  166.       last[pos] = length;
  167.       if (++pos15 == TIME15) 
  168.     {
  169.       pos15 = 0;
  170.     }
  171.       if (++pos5 == TIME15) 
  172.     {
  173.       pos5 = 0;
  174.     }
  175.       if (++pos1 == TIME15) 
  176.     {
  177.       pos1 = 0;
  178.     }
  179.       if (++pos == TIME15) 
  180.     {
  181.       pos = 0;
  182.     }
  183.     
  184.       convertTime(sum1, TIME1, uptimestr + 6);
  185.       convertTime(sum5, TIME5, uptimestr + 12);
  186.       convertTime(sum15, TIME15, uptimestr + 18);
  187.     
  188.       SetWindowTitles(win, uptimestr, MyScreenTitle);
  189.     }
  190.   
  191.  done:
  192.   if (win) 
  193.     CloseWindow(win);
  194.   if (IntuitionBase) 
  195.     CloseLibrary(IntuitionBase);
  196.   if (ExecBase) 
  197.     CloseLibrary(ExecBase);
  198. }
  199.  
  200.  
  201.  
  202.  
  203.